package mobi.greenplate.client.handler;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import mobi.greenplate.client.converter.CommunityConverter;
import mobi.greenplate.client.domain.Community;
import mobi.greenplate.client.parser.DomainHandler;

/**
 *
 * @author dmakariev
 */
public class CommunityListHandler extends DomainHandler<List<Community>> {

    private static final String ADDRESS_TAG = "community";
    private static final String ADDRESS_BODY = "//community-list/community";
    private final List<Community> communityList = new ArrayList<Community>();
    private Community currentCommunity;

    @Override
    public List<Community> getEntity() {
        return Collections.unmodifiableList(communityList);
    }

    @Override
    public void endTag(String parentPath, String tagName, String text) {
        if (parentPath.equalsIgnoreCase(ADDRESS_BODY)) {
            currentCommunity = CommunityConverter.endTag(currentCommunity, tagName, text);
        }
    }

    @Override
    public void startTag(String parentPath, String tagName) {
        if (tagName.equalsIgnoreCase(ADDRESS_TAG)) {
            currentCommunity = new Community();
            communityList.add(currentCommunity);
        }
    }
}
